<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use app\model\home\Assistant as assistantModel;
use app\model\home\Duty;
use think\App;
use think\db\Where;
use \Throwable;

class Assistant extends BaseController
{
    protected $model;

    public function __construct(App $app, assistantModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 随机列表
     */
    public function random()
    {
        $where = [];
        $where['delete_time'] = 0;
        $where['status'] = 1;
        $where['is_recommend'] = 1;
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $order = null;
        $fields = 'id,nickname,address,tag,per_price,id imageArr,
        store_id,sign,score,store_id store_name,avatar,cover,hot,create_time';
        return $this->model->getRandomList($where, $fields, $order, $page, $pageSize);
    }

    /**
     * 列表查询
     * @return array
     */
    public function query()
    {
        $where = [];
        $where[] = ['delete_time', '=', 0];
        $where[] = ['status', '=', 1];
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $order = null;
        $storeId = input('store_id/s', '');
        $params = [];
//        $storeId && $params[] = ['store_id', 'in', explode(',', $storeId)];
        $nickName = input('nickname');
        $dutyDate = input('duty_date');
        empty($dutyDate) && $dutyDate = date('Y-m-d', time());
        //获取当天门店值班的助教
        $map[] = ['store_id', '=', $storeId];
        $map[] = ['duty_date', '=', $dutyDate];
        $map[] = ['duty_status', '=', 1];
        $map[] = ['re_type', '=', 1];
        $map[] = ['status', 'in', [1, 2]];
        $assistantIds = Duty::where([$map])->column('re_id');
        if (empty($assistantIds)) {
            $params = [];
        } else {
            if (!empty($assistantIds) && is_array($assistantIds)) {
                $params[] = ['id', 'in', $assistantIds];
            }
            if (!empty($nickName)) {
                $params[] = ['nickname', 'like', '%' . $nickName . '%'];
            }
        }
        $fields = 'id,nickname,address,profile,tag,per_price,id imageArr,
        store_id,sign,score,store_id store_name,grade_id grade,star,avatar,cover,hot,create_time';
        return $this->model->getList($where, $fields, $order, $page, $pageSize, $params);
    }

    /**
     * 显示内容
     * @return array
     */
    public function detail()
    {
        try {
            return $this->model->getOne($this->params, 'id,nickname,address,tag,per_price,id imageArr,
        store_id,sign,profile,score,store_id store_name,grade_id grade,star,avatar,cover,hot,is_vip,create_time');
        } catch (Throwable $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 新增
     */
    public function create()
    {
        try {
            validate(\app\validate\home\Assistant::class)
                ->scene('create')
                ->check($this->params);
            $this->params['member_id'] = $this->memberId;
            return $this->model->add($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }
}
